﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mathhammer40K.Data;

namespace Mathhammer40K
{
    public partial class Roller : Form
    {
        public Roller()
        {
            InitializeComponent();
        }

        private void battleToolStripMenuItem_Click(object sender, EventArgs e)
        {           
            CombatEngine engine = new CombatEngine();

            Int32 battleCount = 1;
            if(!Int32.TryParse(toolStripTextBoxBattleNumber.Text, out battleCount))
                battleCount = 1;

            engine.ComputeBattleDistribution(combatInputs, combatResults, battleCount);

            statusLabel.Text = "Computed " + battleCount.ToString() + " battles";
        }

        private void unitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CombatInputs.UnitsRow unit = combatInputs.Units.NewUnitsRow();

            FormAddUnit addUnit = new FormAddUnit(unit);
            addUnit.ShowDialog();

            if (!unit.IsNameNull() && unit.Name != "INVALID")
            {
                combatInputs.Units.AddUnitsRow(unit);
                statusLabel.Text = "Unit updated";
            }
                       
        }

        private void selectedUnitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataRowView dataItem = null;
            if (unitsDataGridView.SelectedCells.Count > 0)
            {
                dataItem = unitsDataGridView.SelectedCells[0].OwningRow.DataBoundItem as DataRowView;
            }
            else if (unitsDataGridView.SelectedRows.Count > 0)               
            {
                dataItem = unitsDataGridView.SelectedRows[0].DataBoundItem as DataRowView;
            }

            if(dataItem != null)
            {
                CombatInputs.UnitsRow unit = dataItem.Row as CombatInputs.UnitsRow;
                FormAddUnit addUnit = new FormAddUnit(unit);
                addUnit.ShowDialog();

                if (unit.Name == "INVALID")
                {
                    if (combatInputs.Units.Contains(unit))
                        combatInputs.Units.RemoveUnitsRow(unit);
                }
                statusLabel.Text = "Unit updated";
            }
        }

        private void unitsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = saveFileDialog.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                String path = saveFileDialog.FileName;
                combatInputs.WriteXml(path, XmlWriteMode.WriteSchema);
                statusLabel.Text = "Saved to " + path;
            }
        }

        private void unitsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DialogResult res = openFileDialog.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                String path = openFileDialog.FileName;
                combatInputs.Clear();

                combatInputs.ReadXml(path, XmlReadMode.ReadSchema);

                statusLabel.Text = "Loaded from " + path;
            }
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            combatResults.Clear();
        }

        private void shootingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CombatEngine engine = new CombatEngine();

            Int32 battleCount = 1;
            if (!Int32.TryParse(toolStripTextShootingRounds.Text, out battleCount))
                battleCount = 1;

            Int32 side = 1;
            if(!Int32.TryParse(toolStripComboSide.Text, out side))
                side = 1;

            engine.ComputeBattleDistribution(combatInputs, combatResults, battleCount, false, side);

            statusLabel.Text = "Computed " + battleCount.ToString() + " battles";
        }

        private void selectedUnitToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            System.Collections.Generic.List<DataRowView> dataItems = new List<DataRowView>(); ;
            if (unitsDataGridView.SelectedCells.Count > 0)
            {
                for (int i = 0; i < unitsDataGridView.SelectedCells.Count; i++)
                {
                    dataItems.Add(unitsDataGridView.SelectedCells[i].OwningRow.DataBoundItem as DataRowView);
                }
            }
            else if (unitsDataGridView.SelectedRows.Count > 0)
            {
                for (int i = 0; i < unitsDataGridView.SelectedRows.Count; i++)
                {
                    dataItems.Add(unitsDataGridView.SelectedRows[i].DataBoundItem as DataRowView);
                }
            }

            if (dataItems.Count > 0)
            {
                CombatInputs tempDataSet = new CombatInputs();
                foreach (DataRowView dataItem in dataItems)
                {
                    CombatInputs.UnitsRow unit = dataItem.Row as CombatInputs.UnitsRow;
                    tempDataSet.Units.CopyAndAdd(unit);
                }

                DialogResult res = saveFileDialog.ShowDialog();
                String path = saveFileDialog.FileName;
                
                if (res == System.Windows.Forms.DialogResult.OK)
                {
                    tempDataSet.WriteXml(path, XmlWriteMode.WriteSchema);
                    tempDataSet.Clear();
                    statusLabel.Text = dataItems.Count + " items saved to " + path;
                }
            }

            
        }

        private void singleUnitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = openFileDialog.ShowDialog();
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                String path = openFileDialog.FileName;
                CombatInputs tempDataSet = new CombatInputs();

                tempDataSet.ReadXml(path, XmlReadMode.ReadSchema);

                foreach (CombatInputs.UnitsRow row in tempDataSet.Units)
                    combatInputs.Units.CopyAndAdd(row);

                statusLabel.Text = "Loaded from " + path;
            }
        }

        private void toolStripMoveUp_Click(object sender, EventArgs e)
        {

            System.Windows.Forms.DataGridViewRow dataItem = null;
            if (unitsDataGridView.SelectedCells.Count > 0)
            {
                dataItem = unitsDataGridView.SelectedCells[0].OwningRow as System.Windows.Forms.DataGridViewRow;
            }
            else if (unitsDataGridView.SelectedRows.Count > 0)
            {
                dataItem = unitsDataGridView.SelectedRows[0] as System.Windows.Forms.DataGridViewRow;
            }

            if (dataItem != null)
            {
                int index = unitsDataGridView.Rows.IndexOf(dataItem);                   
                if (index > 0)
                {
                    CombatInputs.UnitsRow upperRow = (unitsDataGridView.Rows[index - 1].DataBoundItem as DataRowView).Row as CombatInputs.UnitsRow;
                    CombatInputs.UnitsRow lowerRow = (dataItem.DataBoundItem as DataRowView).Row as CombatInputs.UnitsRow;

                    Int32 temp = upperRow.ID;
                    Int32 temp2 = lowerRow.ID;
                    upperRow.ID = Int32.MaxValue;
                    lowerRow.ID = Int32.MaxValue - 1;
                    upperRow.ID = temp2;
                    lowerRow.ID = temp;
                                       
                }
            }

            unitsDataGridView.Sort(unitsDataGridView.Columns["ID"], ListSortDirection.Ascending);
        }

        private void toolStripMoveDown_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.DataGridViewRow dataItem = null;
            if (unitsDataGridView.SelectedCells.Count > 0)
            {
                dataItem = unitsDataGridView.SelectedCells[0].OwningRow as System.Windows.Forms.DataGridViewRow;
            }
            else if (unitsDataGridView.SelectedRows.Count > 0)
            {
                dataItem = unitsDataGridView.SelectedRows[0] as System.Windows.Forms.DataGridViewRow;
            }

            if (dataItem != null)
            {
                int index = unitsDataGridView.Rows.IndexOf(dataItem);
                if (index < unitsDataGridView.Rows.Count-2)
                {
                    CombatInputs.UnitsRow upperRow = (dataItem.DataBoundItem as DataRowView).Row as CombatInputs.UnitsRow;
                    CombatInputs.UnitsRow lowerRow = (unitsDataGridView.Rows[index + 1].DataBoundItem as DataRowView).Row as CombatInputs.UnitsRow;

                    Int32 temp = upperRow.ID;
                    Int32 temp2 = lowerRow.ID;
                    upperRow.ID = Int32.MaxValue;
                    lowerRow.ID = Int32.MaxValue - 1;
                    upperRow.ID = temp2;
                    lowerRow.ID = temp;
                }
            }

            unitsDataGridView.Sort(unitsDataGridView.Columns["ID"], ListSortDirection.Ascending);
        }

        private void toolStripButtonDuplicate_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.DataGridViewRow dataItem = null;
            if (unitsDataGridView.SelectedCells.Count > 0)
            {
                dataItem = unitsDataGridView.SelectedCells[0].OwningRow as System.Windows.Forms.DataGridViewRow;
            }
            else if (unitsDataGridView.SelectedRows.Count > 0)
            {
                dataItem = unitsDataGridView.SelectedRows[0] as System.Windows.Forms.DataGridViewRow;
            }

            if (dataItem != null)
            {               
                CombatInputs.UnitsRow row = (dataItem.DataBoundItem as DataRowView).Row as CombatInputs.UnitsRow;

                CombatInputs.UnitsRow newRow = combatInputs.Units.NewUnitsRow();
                newRow.Copy(row);
                
                combatInputs.Units.AddUnitsRow(newRow);
            }
        }

    }
}
